#!/bin/bash

username=$1
port=$2
ipaddress=($3)
hostname=($4)
installpath=$5
xmlpath=$6

rm -f $xmlpath
touch $xmlpath
nodeNames=${hostname[0]}
backIp1s=${ipaddress[0]}
dataNode1=$installpath"/cluster/dn1"
if [ ${#hostname[*]} -gt 1 ]; then
    for i in $(seq 1 $[${#hostname[*]}-1])
    do
        nodeNames=$nodeNames","${hostname[i]}
        backIp1s=$backIp1s","${ipaddress[i]}
        dataNode1=$dataNode1","${hostname[i]}","$installpath"/cluster/dn1"
    done
fi
echo '<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
    <CLUSTER>
        <PARAM name="clusterName" value="'$username'"/>
        <PARAM name="nodeNames" value="'$nodeNames'"/>
        <PARAM name="backIp1s" value="'$backIp1s'"/>
        <PARAM name="gaussdbAppPath" value="'$installpath'/cluster/app"/>
        <PARAM name="gaussdbLogPath" value="'$installpath'/cluster/gaussdb_log"/>
        <PARAM name="tmpMppdbPath" value="'$installpath'/cluster/tmp"/>
        <PARAM name="gaussdbToolPath" value="'$installpath'/cluster/tool"/>
        <PARAM name="corePath" value="'$installpath'/cluster/corefile"/>
        <PARAM name="clusterType" value="single-inst"/>
    </CLUSTER>
    <DEVICELIST>
        <DEVICE sn="'${hostname[0]}'">
            <PARAM name="name" value="'${hostname[0]}'"/>
            <PARAM name="azName" value="AZ1"/>
            <PARAM name="azPriority" value="1"/>
            <PARAM name="backIp1" value="'${ipaddress[0]}'"/>
            <PARAM name="sshIp1" value="'${ipaddress[0]}'"/>
            <PARAM name="dataNum" value="1"/>
            <PARAM name="dataPortBase" value="'$port'"/>
            <PARAM name="dataNode1" value="'$dataNode1'"/>
        </DEVICE>' > $xmlpath
if [ ${#hostname[*]} -gt 1 ]; then
    sed -i '23i \            <PARAM name="dataPortStandby" value="'$port'"/>' $xmlpath
    for i in $(seq 1 $[${#hostname[*]}-1])
    do
        echo '        <DEVICE sn="'${hostname[i]}'">' >> $xmlpath
        echo '            <PARAM name="name" value="'${hostname[i]}'"/>' >> $xmlpath
        echo '            <PARAM name="azName" value="AZ1"/>' >> $xmlpath
        echo '            <PARAM name="azPriority" value="1"/>' >> $xmlpath
        echo '            <PARAM name="backIp1" value="'${ipaddress[i]}'"/>' >> $xmlpath
        echo '            <PARAM name="sshIp1" value="'${ipaddress[i]}'"/>' >> $xmlpath
        echo '        </DEVICE>' >> $xmlpath
    done
fi
echo '    </DEVICELIST>
</ROOT>' >> $xmlpath